class Solution:
    def isAdditiveNumber(self, num: str) -> bool:
        for i in range(len(num) - 2):
            if i!=0 and num[0]=='0':
                break
            n1 = int(num[0:i + 1])
            for j in range(i + 1, len(num) - 1):
                if j != i+1 and '0' == num[i + 1]:
                    break
                if len(num) - (j + 1) < max(i+1, j-i):
                    break
                n2 = int(num[i + 1:j + 1])
                if self.check(num, n1, n2, j + 1):
                    return True
        return False

    def check(self, num, n1, n2, lastIndex):
        n3 = str(n1 + n2)
        if len(num) - lastIndex < len(n3):
            return False
        for i in range(len(n3)):
            if n3[i] != num[lastIndex + i]:
                return False

        if len(num) - lastIndex == len(n3):
            return True
        return self.check(num, n2, int(n3), lastIndex + len(n3))


if __name__ == '__main__':
    so = Solution()
    m = "198019823962"
    r = so.isAdditiveNumber(m)
    print(r)
